package android.coocoo.common;

import java.util.ArrayList;

import android.coocoo.models.TaskInfo;

public class Util {
	
	// Rearrange tasksList by
	//	1. Date.
	//	2. Time.
	//	3. Priority (high to low).
	//	4. Category.
	//	5. Title.
	public static boolean reArrangeTasksList(ArrayList<TaskInfo> tasksList){
		if (tasksList != null){
			int i, j;
			for (i=0; i<(tasksList.size()-1); i++)
				for (j=i+1; j<tasksList.size(); j++){
					if (tasksList.get(i).getStartTime().getYear() > tasksList.get(j).getStartTime().getYear())
						exchange(tasksList, i, j);
					else if (tasksList.get(i).getStartTime().getYear() == tasksList.get(j).getStartTime().getYear()){
						if (tasksList.get(i).getStartTime().getMonth() > tasksList.get(j).getStartTime().getMonth())
							exchange(tasksList, i, j);
						else if (tasksList.get(i).getStartTime().getMonth() == tasksList.get(j).getStartTime().getMonth()){
							if (tasksList.get(i).getStartTime().getDate() > tasksList.get(j).getStartTime().getDate())
								exchange(tasksList, i, j);
							else if (tasksList.get(i).getStartTime().getDate() == tasksList.get(j).getStartTime().getDate()){
								if (tasksList.get(i).getStartTime().getHours() > tasksList.get(j).getStartTime().getHours())
									exchange(tasksList, i, j);
								else if (tasksList.get(i).getStartTime().getHours() == tasksList.get(j).getStartTime().getHours()){
									if (tasksList.get(i).getStartTime().getMinutes() > tasksList.get(j).getStartTime().getMinutes())
										exchange(tasksList, i, j);
									else if (tasksList.get(i).getStartTime().getMinutes() == tasksList.get(j).getStartTime().getMinutes()){
										if (tasksList.get(i).getPriority() < tasksList.get(j).getPriority())
											exchange(tasksList, i, j);
										else if (tasksList.get(i).getPriority() == tasksList.get(j).getPriority()){
											if (tasksList.get(i).getCategoryId() > tasksList.get(j).getCategoryId())
												exchange(tasksList, i, j);
											else if (tasksList.get(i).getCategoryId() == tasksList.get(j).getCategoryId()){
												if (tasksList.get(i).getTitle().compareToIgnoreCase(tasksList.get(j).getTitle()) > 0)
													exchange(tasksList, i, j);
											}
										}
									}
								}
							} 
						}
					}
						
				}
			return true;
		}
		return false;
	}

	private static boolean exchange(ArrayList<TaskInfo> tasksList, int i, int j) {
		// TODO Auto-generated method stub
		if (tasksList!=null)
			if (tasksList.size()>i && tasksList.size()>j && i>=0 && j>=0 && i!=j ){
				TaskInfo tTask = tasksList.get(i);
				tasksList.set(i, tasksList.get(j));
				tasksList.set(j, tTask);
				return true;
			}
		return false;
	}
}
